﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;

namespace FortnoxAPI
{
    /// <summary>
    /// Get Project from Fortnox
    /// </summary>
    public partial class GetSupplierregister
    {

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="token">The token.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        public static DataTable GetValues(string url, string token, string db)
        {
            XDocument doc = XDocument.Load(url + "token=" + token + "&db=" + db);

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("id", typeof(string));
            dt1.Columns.Add("supplierno", typeof(string));
            dt1.Columns.Add("name", typeof(string));
            dt1.Columns.Add("address", typeof(string));
            dt1.Columns.Add("address2", typeof(string));
            dt1.Columns.Add("visitaddress", typeof(string));
            dt1.Columns.Add("zip", typeof(string));
            dt1.Columns.Add("city", typeof(string));
            dt1.Columns.Add("country", typeof(string));
            dt1.Columns.Add("phone", typeof(string));
            dt1.Columns.Add("fax", typeof(string));
            dt1.Columns.Add("email", typeof(string));
            dt1.Columns.Add("orgno", typeof(string));
            dt1.Columns.Add("vatno", typeof(string));
            dt1.Columns.Add("paycond", typeof(string));
            dt1.Columns.Add("supplierpaycond", typeof(string));
            dt1.Columns.Add("delcond", typeof(string));
            dt1.Columns.Add("preacct", typeof(string));
            dt1.Columns.Add("bg", typeof(string));
            dt1.Columns.Add("pg", typeof(string));
            dt1.Columns.Add("ICA", typeof(string));

            IEnumerable<XElement> contacts = from contact in doc.Descendants("contact")

                                             //where (string)post.Attribute("id").Value == "1"
                                             select contact;


            foreach (XElement contact in contacts)
            {
                DataRow dr1 = dt1.NewRow();

                if (contact.Element("id") != null)
                {
                    dr1["id"] = contact.Element("id").Value;
                }
                else
                {
                    dr1["id"] = "";
                }

                if (contact.Element("supplierno") != null)
                {
                    dr1["supplierno"] = contact.Element("supplierno").Value;
                }
                else
                {
                    dr1["supplierno"] = "";
                }

                if (contact.Element("name") != null)
                {
                    dr1["name"] = contact.Element("name").Value;
                }
                else
                {
                    dr1["name"] = "";
                }

                if (contact.Element("address") != null)
                {
                    dr1["address"] = contact.Element("address").Value;
                }
                else
                {
                    dr1["address"] = "";
                }

                if (contact.Element("address2") != null)
                {
                    dr1["address2"] = contact.Element("address2").Value;
                }
                else
                {
                    dr1["address2"] = "";
                }

                if (contact.Element("visitaddress") != null)
                {
                    dr1["visitaddress"] = contact.Element("visitaddress").Value;
                }
                else
                {
                    dr1["visitaddress"] = "";
                }

                if (contact.Element("zip") != null)
                {
                    dr1["zip"] = contact.Element("zip").Value;
                }
                else
                {
                    dr1["zip"] = "";
                }

                if (contact.Element("city") != null)
                {
                    dr1["city"] = contact.Element("city").Value;
                }
                else
                {
                    dr1["city"] = "";
                }

                if (contact.Element("country") != null)
                {
                    dr1["country"] = contact.Element("country").Value;
                }
                else
                {
                    dr1["country"] = "";
                }

                if (contact.Element("phone") != null)
                {
                    dr1["phone"] = contact.Element("phone").Value;
                }
                else
                {
                    dr1["phone"] = "";
                }

                if (contact.Element("fax") != null)
                {
                    dr1["fax"] = contact.Element("fax").Value;
                }
                else
                {
                    dr1["fax"] = "";
                }

                if (contact.Element("email") != null)
                {
                    dr1["email"] = contact.Element("email").Value;
                }
                else
                {
                    dr1["email"] = "";
                }

                if (contact.Element("orgno") != null)
                {
                    dr1["orgno"] = contact.Element("orgno").Value;
                }
                else
                {
                    dr1["orgno"] = "";
                }

                if (contact.Element("vatno") != null)
                {
                    dr1["vatno"] = contact.Element("vatno").Value;
                }
                else
                {
                    dr1["vatno"] = "";
                }

                if (contact.Element("paycond") != null)
                {
                    dr1["paycond"] = contact.Element("paycond").Value;
                }
                else
                {
                    dr1["paycond"] = "";
                }

                if (contact.Element("supplierpaycond") != null)
                {
                    dr1["supplierpaycond"] = contact.Element("supplierpaycond").Value;
                }
                else
                {
                    dr1["supplierpaycond"] = "";
                }

                if (contact.Element("delcond") != null)
                {
                    dr1["delcond"] = contact.Element("delcond").Value;
                }
                else
                {
                    dr1["delcond"] = "";
                }

                if (contact.Element("preacct") != null)
                {
                    dr1["preacct"] = contact.Element("preacct").Value;
                }
                else
                {
                    dr1["preacct"] = "";
                }

                if (contact.Element("bg") != null)
                {
                    dr1["bg"] = contact.Element("bg").Value;
                }
                else
                {
                    dr1["bg"] = "";
                }

                if (contact.Element("pg") != null)
                {
                    dr1["pg"] = contact.Element("pg").Value;
                }
                else
                {
                    dr1["pg"] = "";
                }

                if (contact.Element("ICA") != null)
                {
                    dr1["ICA"] = contact.Element("ICA").Value;
                }
                else
                {
                    dr1["ICA"] = "";
                }
                dt1.Rows.Add(dr1);
            }
            return dt1;
        }

    }
}
